<template>
  <div id="home">
    <div style="margin: 10px 0">
      <el-button type="primary" @click="add">新增</el-button>
    </div>
    <div style="margin: 10px 0">
      <el-input v-model="search" placeholder="请输入关键字" style="width: 20%"></el-input>
      <el-button type="primary" @click="load">查询</el-button>
    </div>
    <el-table
      :data="tableData"
      border
      style="width: 100%">
      <el-table-column
        fixed
        prop="id"
        label="id"
        width="150">
      </el-table-column>
      <el-table-column
        prop="name"
        label="姓名"
        width="120">
      </el-table-column>

      <el-table-column fixed="right" label="操作" width="100">
        <template slot-scope="scope">
          <el-button @click="handleClick(scope.row)" type="text">编辑</el-button>
          <el-popconfirm title="确定删除吗" @confirm="handleDelete(scope.row.id)">
            <template #reference>
              <el-button slot="reference" type="danger" >删除</el-button>
            </template>
          </el-popconfirm>
        </template>

      </el-table-column>
    </el-table>
    <div style="margin: 10px 0">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage4"
        :page-sizes="[5, 10, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
      <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
        <el-form :model="form"  label-width="120px">
          <el-form-item label="用户名">
            <el-input v-model="form.username"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="save">确 定</el-button>
  </span>
      </el-dialog>

    </div>
  </div>
</template>

<script>

  import request from '@/utils/request'
  export default {
    name: 'Role',
    components:{

    },
    data(){
      return{
        form: {},
        dialogVisible: false,
        search: '',
        currentPage4: 1,
        pageSize: 5,
        total:0,
        tableData: [

        ]
      }
    },
    created () {
      this.load()
    },
    methods: {
      load(){
        request.get("http://localhost:9090/user",{
          params: {
            pageNum: this.currentPage4,
            pageSize:this.pageSize,
            search: this.search
          }

        }).then(res =>{
          console.log(res)
          this.tableData=res.data.records
          this.total=res.data.total
        })
      },
      add(){
        this.dialogVisible=true
        this.form ={}
      },
      save() {

        request.put("http://localhost:9090/role",this.form).then(res => {
          console.log(res)
        })

      },
      handleClick(row){
        this.form = JSON.parse(JSON.stringify(row))
        this.dialogVisible =true
      },
      handleDelete(id){
        request.delete("/api/role/"+id).then(res =>{
          if(res.code=="0"){
            this.$message({
              type: "success",
              message: "删除成功"
            })
          }else {
            this.$message({
              type: "error",
              message: res.msg
            })
          }
          this.load()  //重新加载
        })
      },
      handleSizeChange(pageSize){  //改变当前每页的个数触发
        this.pageSize =pageSize
        this.load()
      },
      handleCurrentChange(pageNum){  //改变当前页码触发
        this.currentPage4=pageNum
        this.load()
      }
    }
  }
</script>

<style scoped>

</style>
